package com.ny.service.impl;

import com.ny.mapper.MarketingActivityMapper;
import com.ny.mapper.MemberMapper;
import com.ny.mapper.ProductPurchaseMapper;
import com.ny.domain.MarketingActivityDO;
import com.ny.domain.MemberDO;
import com.ny.domain.ProductPurchaseDO;
import com.ny.dto.MarketingActivityDTO;
import com.ny.dto.PageResult;
import com.ny.query.MarketingActivityQuery;
import com.ny.query.MemberQuery;
import com.ny.service.MarketingService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import java.util.Date;

/**
 * 营销工具模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class MarketingServiceImpl implements MarketingService {

    @Autowired
    private MarketingActivityMapper marketingActivityMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ProductPurchaseMapper productPurchaseMapper;

    @Override
    public Boolean publishMarketingActivity(MarketingActivityDTO marketingActivityDTO) {
        // 检查活动名称是否已存在
        MarketingActivityDO existingActivity = marketingActivityMapper.selectByName(marketingActivityDTO.getActivityName());
        if (existingActivity != null) {
            throw new BusinessException("营销活动名称已存在，请选择其他名称。");
        }
        // 将营销活动信息保存到数据库
        MarketingActivityDO marketingActivityDO = new MarketingActivityDO();
        marketingActivityDO.setActivityName(marketingActivityDTO.getActivityName());
        marketingActivityDO.setActivityDescription(marketingActivityDTO.getActivityDescription());
        marketingActivityDO.setStartDate(marketingActivityDTO.getStartDate());
        marketingActivityDO.setEndDate(marketingActivityDTO.getEndDate());
        marketingActivityDO.setCreateBy("system");
        marketingActivityDO.setCreateTime(new Date());
        marketingActivityDO.setUpdateBy("system");
        marketingActivityDO.setUpdateTime(new Date());
        marketingActivityMapper.insert(marketingActivityDO);
        return true;
    }

    @Override
    public Boolean updateMarketingActivity(MarketingActivityDTO marketingActivityDTO) {
        // 检查活动ID是否存在
        MarketingActivityDO existingActivity = marketingActivityMapper.selectById(marketingActivityDTO.getActivityId());
        if (existingActivity == null) {
            throw new BusinessException("营销活动ID不存在，请重新输入。");
        }
        // 更新营销活动信息到数据库
        MarketingActivityDO marketingActivityDO = new MarketingActivityDO();
        marketingActivityDO.setActivityId(marketingActivityDTO.getActivityId());
        marketingActivityDO.setActivityName(marketingActivityDTO.getActivityName());
        marketingActivityDO.setActivityDescription(marketingActivityDTO.getActivityDescription());
        marketingActivityDO.setStartDate(marketingActivityDTO.getStartDate());
        marketingActivityDO.setEndDate(marketingActivityDTO.getEndDate());
        marketingActivityDO.setUpdateBy("system");
        marketingActivityDO.setUpdateTime(new Date());
        marketingActivityMapper.updateById(marketingActivityDO);
        return true;
    }

    @Override
    public Boolean deleteMarketingActivity(MarketingActivityDTO marketingActivityDTO) {
        // 检查活动ID是否存在
        MarketingActivityDO existingActivity = marketingActivityMapper.selectById(marketingActivityDTO.getActivityId());
        if (existingActivity == null) {
            throw new BusinessException("营销活动ID不存在，请重新输入。");
        }
        // 删除营销活动信息
        marketingActivityMapper.deleteById(marketingActivityDTO.getActivityId());
        return true;
    }

    @Override
    public PageResult<MarketingActivityDO> listMarketingActivities(MarketingActivityQuery marketingActivityQuery) {
        // 根据输入的开始日期和结束日期查询营销活动列表
        int totalRecords = marketingActivityMapper.countByDateRange(marketingActivityQuery.getStartDate(), marketingActivityQuery.getEndDate());
        long totalPages = totalRecords / marketingActivityQuery.getPageSize() + (totalRecords % marketingActivityQuery.getPageSize() > 0 ? 1 : 0);
        List<MarketingActivityDO> marketingActivities = marketingActivityMapper.pageByDateRange(marketingActivityQuery.getStartDate(), marketingActivityQuery.getEndDate(), marketingActivityQuery.getPageIndex(), marketingActivityQuery.getPageSize());
        PageResult<MarketingActivityDO> pageResult = new PageResult<MarketingActivityDO>(totalPages, totalRecords, marketingActivityQuery.getPageSize(),marketingActivities);
        return pageResult;
    }

    @Override
    public ProductPurchaseDO getMemberConsumptionAnalysis(MemberQuery memberQuery) {
        // 检查会员ID是否存在
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员ID不存在，请重新输入。");
        }
        // 根据输入的会员ID和分析日期获取会员消费分析
        ProductPurchaseDO productPurchaseDO = productPurchaseMapper.selectByMemberIdAndDate(memberQuery.getMemberId(), memberQuery.getAnalysisDate());
        return productPurchaseDO;
    }

    @Override
    public List<ProductPurchaseDO> getAllMemberConsumptionAnalysis(MemberQuery memberQuery) {
        // 根据输入的分析日期获取所有会员的消费分析
        List<ProductPurchaseDO> productPurchaseDOList = productPurchaseMapper.selectAllByDate(memberQuery.getAnalysisDate());
        return productPurchaseDOList;
    }

    @Override
    public MemberDO getMemberInfo(MemberQuery memberQuery) {
        // 检查会员ID是否存在
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员ID不存在，请重新输入。");
        }
        // 根据输入的会员ID查询会员基本信息
        return memberDO;
    }

    @Override
    public List<ProductPurchaseDO> getMemberConsumptionRecord(MemberQuery memberQuery) {
        // 检查会员ID是否存在
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员ID不存在，请重新输入。");
        }
        // 根据输入的会员ID查询会员消费记录
        List<ProductPurchaseDO> productPurchaseDOList = productPurchaseMapper.selectAllByMemberId(memberQuery.getMemberId());
        return productPurchaseDOList;
    }

    @Override
    public List<ProductPurchaseDO> getMemberPurchaseRecord(MemberQuery memberQuery) {
        // 检查会员ID是否存在
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员ID不存在，请重新输入。");
        }
        // 根据输入的会员ID查询会员购买记录
        List<ProductPurchaseDO> productPurchaseDOList = productPurchaseMapper.selectAllByMemberId(memberQuery.getMemberId());
        return productPurchaseDOList;
    }

    @Override
    public Integer getMemberLevelInfo(MemberQuery memberQuery) {
        // 检查会员ID是否存在
        MemberDO memberDO = memberMapper.selectById(memberQuery.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员ID不存在，请重新输入。");
        }
        // 根据输入的会员ID查询会员等级信息
        return memberDO.getLevelId();
    }
}
